package com.zk.shop.service.Impl;

import com.zk.shop.dao.GoodsDao;
import com.zk.shop.entity.Goods;
import com.zk.shop.service.GoodsService;
import com.zk.shop.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * 关轶阳
 */

public class GoodsServicesImpl implements GoodsService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    GoodsDao goodsDao = session.getMapper(GoodsDao.class);

    /**
     * 1、加载时获取所有商品列表
     *
     * @return
     */
    @Override
    public List<Goods> getAllgoods(Map map) {
        return goodsDao.getAllgoods(map);
    }

    /**
     * 2、根据商品分类所传过来的分类ID所进行遍历
     *
     * @param map
     * @return
     */
    @Override
    public List<Goods> getAllgoodsById(Map map) {
        return goodsDao.getAllgoodsById(map);
    }

    /**
     * 3、发布新商品
     *
     * @param goods
     * @return
     */
    @Override
    public int AddGoods(Goods goods) {
        int flag = 0;
        try {
            flag = goodsDao.AddGoods(goods);
            if (flag > 0) {
                session.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     * 4、根据商品Id进行删除
     *
     * @param goodsId
     * @return
     */
    @Override
    public boolean deleteGoods(int goodsId) {
        boolean flag = false;
        try {
            flag = goodsDao.deleteGoods(goodsId);
            if (flag) {
                session.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     * 5、根据关键字搜索商品
     *
     * @param map
     * @return
     *//*
    @Override
    public List<Goods> getAllGoodsByKeyword(Map map) {
        return goodsDao.getAllGoodsByKeyword(map);
    }*/

    /**
     * 6、点击该商品时显示该商品详情
     *
     * @param goodsId
     * @return
     */
    public Goods getThisGoodsById(int goodsId) {
        Goods goods = null;
        try {
            SqlSession session = SqlSessionFactoryUtil.getSqlSession();
            GoodsDao goodsDao = session.getMapper(GoodsDao.class);
            goods = goodsDao.getThisGoodsById(goodsId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return goods;
    }

    /**
     * 7、获取商品总条数
     *
     * @return
     */
    @Override
    public int getGoodsCount() {
        return goodsDao.getGoodsCount();
    }

    /**
     * 8、获取所有服装类商品并在主页展示
     * @param
     * @return
     */
    public List<Goods> getAllGoodsByClothes(){
        return goodsDao.getAllGoodsByClothes();
    }

    /**
     * 9、获取所有杂货类商品并在主页展示
     * @return
     */
    public List<Goods> getAllGoodsBySundry(){
        return goodsDao.getAllGoodsBySundry();
    }
    /**
     *10、 获取所有数码类商品并在主页展示
     * @return
     */
    public List<Goods> getAllGoodsByDigital(){
        return goodsDao.getAllGoodsByDigital();
    }
    /**
     *11、 获取所有运动类商品并在主页展示
     * @return
     */
    public List<Goods> getAllGoodsByExercise(){
        return goodsDao.getAllGoodsByExercise();
    }
}
